package com.example.bootdemo.atomic;


import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

public class AuctionCustomer implements Runnable {
    private final AtomicReference<CarLicenseTag> carLicenseTagReference;
    private final CarLicenseTag carLicenseTag;
    private final String customerNo;

    public AuctionCustomer(AtomicReference<CarLicenseTag> carLicenseTagReference, CarLicenseTag carLicenseTag, int customerNo) {
        this.carLicenseTagReference = carLicenseTagReference;
        this.carLicenseTag = carLicenseTag;
        this.customerNo = "第" + customerNo + "位客户";
    }

    public void run() {
        // 客户竞拍行为 (模拟竞拍思考准备时间4秒钟)
        try {
            Thread.sleep(new Random().nextInt(4000));
        } catch (Exception e) {
        }

        // 举牌更新最新的竞拍价格
        // 此处做原子引用更新
        boolean bool = carLicenseTagReference.compareAndSet(carLicenseTag,
                new CarLicenseTag(new Random().nextInt(1000)));
        System.out.println("第" + customerNo + "位客户竞拍" + bool + " 当前的竞拍信息" + carLicenseTagReference.get().toString());
    }
}